package com.ccjq.jq173.shosestore.web.controller.shoppingcart;

import com.ccjq.jq173.shosestore.member.po.User;
import com.ccjq.jq173.shosestore.product.po.Product;
import com.ccjq.jq173.shosestore.product.service.impl.productServiceImpl;
import com.ccjq.jq173.shosestore.product.service.productService;
import com.ccjq.jq173.shosestore.shoppingcart.po.ShoppingCart;
import com.ccjq.jq173.shosestore.shoppingcart.service.ShoppingcartService;
import com.ccjq.jq173.shosestore.shoppingcart.service.impl.ShoppingcartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class ShoppingCartServlet extends HttpServlet {
    private static final String TZ_GOCAR = "gocar";  //大标题的购物车
    private static final String TZ_JIESUAN = "jiesuan"; //结算
    private static final String TZ_TIJIAOJIESUAN = "tijiaojiesuan"; //提交结算结算=支付宝/微信
    private static final String TZ_DELETE = "yichu";  //移除
    private static final String TZ_GENGXIN ="gengxin"; //更新
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String tz = request.getParameter("tz");
          if(TZ_GOCAR.equals(tz))
          {
              toGoCar(request,response);
          }
          else if(TZ_JIESUAN.equals(tz))
          {
              toJieSuan(request,response);
          }
          else if(TZ_DELETE.equals(tz))
          {
               toDelete(request,response);
          }
          else if(TZ_GENGXIN.equals(tz))
          {
              toGengXin(request,response);
          }
          else if(TZ_TIJIAOJIESUAN.equals(tz))
          {
              toTiJiaoJieSuan(request,response);
          }
    }

    public static void toGoCar(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        ShoppingcartService ss = new ShoppingcartServiceImpl();
        List<ShoppingCart> shopcartlist = new ArrayList<ShoppingCart>();
        User user=((User)request.getSession().getAttribute("user"));
        if(user == null)
        {
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
            return;
        }
        String userid= user.getUserId();
        System.out.println("打印跳转到购物车的userid="+userid);
        List<ShoppingCart> list1 = ss.queryShoes(userid); //跟据用户id去查购物车里面的信息
        Product pro=null;
        double countmoney =0;
        for(int i=0;i<list1.size();i++)
        {
            ShoppingCart sc = list1.get(i);
            String productId =sc.getProductId();
            productService ps = new productServiceImpl();
            pro = ps.getOneShoes(productId);//根据购物车里的商品id去查，商品的具体信息
            sc.setPrice(pro.getPrice());
            double zs=sc.getQuantity()*sc.getPrice();  //小计 =价格*数量
            sc.setSmallmoney(zs); //小计
            countmoney+=zs;
            sc.setImgpath(pro.getImgpath()); //图片路径
            sc.setFeature(pro.getFeature()); //特性描述
            shopcartlist.add(sc);
        }
        HttpSession session  = request.getSession();
        HttpSession session1  = request.getSession();
        session1.setAttribute("countmoney",countmoney);
        session.setAttribute("shopcartlist",shopcartlist);  //购物车里的商品信息
        session.setAttribute("listlength",shopcartlist.size()+"");
        request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request,response);
    }
    public static void toJieSuan(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/balance.jsp").forward(request,response);
    }
    public static void toDelete(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        ShoppingcartService sc = new ShoppingcartServiceImpl(); //移除
        String shoppingcartId = request.getParameter("shoppingCartId"); //取隐藏域的购物车id;
        boolean b=sc.removeCarShoes(shoppingcartId);
        if(b)  //移除成功就刷新
        {
            request.getSession().setAttribute("yichumessage","删除成功");
            toGoCar(request,response);
        }
        else
        {
            request.getSession().setAttribute("yichumessage","移除失败");
            request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request,response);
        }
    }
    public static void toGengXin(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request,response);
    }
    public static void toTiJiaoJieSuan(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        HttpSession session = request.getSession();
        String name = request.getParameter("receiver");
        String address = request.getParameter("address");
        String sheng = request.getParameter("province");
        String youbian = request.getParameter("zipcode");
        String city = request.getParameter("city");
        String tel = request.getParameter("telphone");
        String qu = request.getParameter("county");
        String payment = request.getParameter("payment");
        double countmoney = (double) session.getAttribute("countmoney");
        String userid= ((User)request.getSession().getAttribute("user")).getUserId();
        ShoppingcartService sc = new ShoppingcartServiceImpl();
        boolean b =sc.jieSuan(name,address,sheng,youbian,city,tel,qu,
                   countmoney+"",Integer.parseInt(payment),userid);
        if(b)
        {  //结算成功，跳转
            request.getRequestDispatcher("order?flag=fol").forward(request,response);
        }
        else
        {
           request.getSession().setAttribute("message","结算失败");
           request.getRequestDispatcher("/WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request,response);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }
}
